package hottargui.standard;

import hottargui.framework.*;

public class StandardBuyState implements TarguiState {

	Game gameContext;
	
	public StandardBuyState(Game game)
	{
		this.gameContext = game;
	}
	
	public boolean buy(int count, Position deploy) throws IllegalActionException {
		boolean retval = false;
		
		if ( count == 0 ) // skip buy
		{
			retval = true;
		}
		else
		{
			Tile deployTarget = gameContext.getTile(deploy);
			Player pit = gameContext.getPlayerInTurn();
			
			if ( pit.getCoins() >= count ) // no credit is given
			{
				if ( gameContext.getGameObjectServer().getBuyStrategy().validPlacement(deployTarget, pit) )
				{
					deployTarget.setUnitCount(deployTarget.getUnitCount() + count);
					((StandardPlayer)pit).withdraw(count);
					
					retval = true;
				}
			}
		}
		
		if ( retval ) // if successful crank the state machine
		{
			gameContext.setState(gameContext.getFactory().getEndTurnState(gameContext));
		}
		
		return retval;
	}

	public State getState() {
		return State.buy;
	}

	public boolean move(Position from, Position to, int count) throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public void newGame() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public void rollDie() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public PlayerColor turnCard() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public PlayerColor getWinner() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}
	
	public void yieldAttack() throws IllegalActionException
	{
		throw new IllegalActionException(this.getState());
	}
	
	public void skip() throws IllegalActionException
	{
		// just set end turn state
		gameContext.setState(gameContext.getFactory().getEndTurnState(gameContext));
	}
}
